$(function () {
  var app = {
    hostSever: getServerHost(),
    url: 'test.json',
    checkSignIn: '/api/longtaitou/checkSignIn',
    getUserCount: '/api/longtaitou/getUserCount',
    signIn: '/api/longtaitou/signIn',
    userSignIn: false,
    signMsg: '',
    openId: GetRequest().openId,
    init: function () {
      this.initInfo();
      $('.btn1').on('click', app.btnEvent);
      closeBar('.closeHave', '.havecard');
    },
    initInfo: function () {
      // 获取打卡信息
      $.get(app.hostSever + app.getUserCount, {
        openId: app.openId
      }).done(function (rp) {
        $('.loading').hide();
        if (200 == rp.code) {
          $('#uCount').text(rp.res.count);
          $('#pHelp').text(rp.res.friend);
        }
      }).fail(function (rp) {})


    },
    btnEvent: function () {
      var index = $(this).index();
      console.log(index);
      if (2 == Number(index)) {
        //打卡弹窗 判断是否打卡
        //判断是否打卡
        $.get(app.hostSever + app.checkSignIn, {
          openId: app.openId
        }).done(function (rp) {
          $('.loading').hide();
          if (200 == rp.code) {
            app.userSignIn = rp.res;
            if (app.userSignIn) {
              autoshowBar('.havecard');
              return;
            } else {
              app.sendKey();
              $('.loading').show();
            }
          }
        }).fail(function (rp) {})

      } else if (3 == Number(index)) {
        location.href = './share.html?openId=' + app.openId;
      }
    },
    //提交打卡记录
    sendKey: function () {
      $('#checkKeyBtn').off('click');
      $.ajax({
        type: 'post',
        url: app.hostSever + app.signIn,
        data: {
          openId: app.openId
        },
        // contentType: 'application/json;charset=UTF-8',
        complete: function () {},
        success: function (rp) {
          if (200 == rp.code) {
            $('.loading').hide();
            autoshowBar('.getCard');
            closeBar('.closeCard', '.getCard');
            $('.getCard').click(function () {
              app.initInfo();
            })
          } else if (999 == rp.code) {
            alert(rp.msg);
          }
        },
        error: function () {}
      })
    }
  };
  app.init();
})